package com.itcast.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.mapper.ClazzMapper;
import com.itcast.pojo.Clazz;
import com.itcast.pojo.ClazzQueryParam;
import com.itcast.pojo.PageResult;
import com.itcast.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzSeiviceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;
    /*
    * 分页查询班级信息
    * */
    @Override
    public PageResult<Clazz> getClazzByPage(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询班级信息",clazzQueryParam);
        //设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> clazzList = clazzMapper.getClazzByPage(clazzQueryParam.getName(), clazzQueryParam.getBegin(), clazzQueryParam.getEnd());
        Page<Clazz> page = (Page<Clazz>) clazzList;

        return new PageResult<>(page.getTotal(),page.getResult());
    }
    /*
    * 根据ID删除班级信息
    * */
    @Override
    public void deleteClazzById(Integer id) {
        log.info("根据ID删除班级信息",id);
        clazzMapper.deleteClazzById(id);
    }
    /*
    * 添加班级信息
    * */
    @Override
    public void addClazz(Clazz clazz) {
        log.info("添加班级信息",clazz);
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);
    }
    /*
     * 根据ID查询班级信息
     * */
    @Override
    public Clazz findClazzById(Integer id) {

        return clazzMapper.findClazzById(id);
    }
    /*
    * 根据id更新班级信息
    * */
    @Override
    public void updateClazzById(Clazz clazz) {
        //设置更新时间
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazzById(clazz);
    }
    /*
    *查询所有班级信息
    * */
    @Override
    public List<Clazz> findAllClazz() {
        return clazzMapper.findAllClazz();
    }
}
